package com.akita.spring.ai.controller;

import com.akita.spring.ai.enums.BizTypeEnum;
import com.akita.spring.ai.repository.ChatHistoryRepository;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai")
public class CustomServiceController {

    @Resource
    private ChatHistoryRepository chatHistoryRepository;

    @Resource
    private ChatClient serviceChatClient;

    @RequestMapping(value = "/service", produces = "text/html;charset=UTF-8")
    public String service(String prompt, String chatId) {
        // 1. 保存会话id
        chatHistoryRepository.save(BizTypeEnum.SERVICE.getType(), chatId);

        // 2. 处理请求
        return serviceChatClient.prompt()
                .user(prompt)
                .advisors(advisorSpec -> advisorSpec.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .call()
                .content();

    }

}
